package cn.t.converter.order;

import cn.t.dto.device.CabinetHandlingOCDTO;
import cn.t.dto.device.vo.OpenCloseDTO;
import cn.t.vo.device.DeviceBaseInfoVO;
import cn.t.vo.user.FirmVO;
import cn.t.vo.user.ParticipantVO;
import cn.t.vo.user.UserSessionVO;
import org.apache.commons.lang3.ObjectUtils;
import org.mapstruct.Mapper;

import java.util.Optional;

/**
 * @author l
 */
@Mapper(componentModel = "spring")
public interface GenericCabinetHandlingConverter {
    OpenCloseDTO dto2VO(CabinetHandlingOCDTO model);

    default void setCloseOpenData(OpenCloseDTO openCloseWeightVO, DeviceBaseInfoVO device, UserSessionVO session){
        openCloseWeightVO.setDeviceName(device.getDeviceName());
        openCloseWeightVO.setFirmId(device.getFirmId());
        openCloseWeightVO.setIsLock(device.getIsLock());
        openCloseWeightVO.setUserId(session.getUserId());
        openCloseWeightVO.setUserName(session.getIndividualVO().getCustomerName());
        openCloseWeightVO.setOperatorInfo(session.operatorInfo());
        openCloseWeightVO.setCardCode(session.getPassportVO().getCardCode());
        openCloseWeightVO.setLoginType(session.getMySaTokenInfoVO().getLoginType());
        openCloseWeightVO.setEmployeeCode(session.getPassportVO().getEmployeeCode());
        //参与者信息
        Optional<FirmVO> first = session.getFirms().stream().filter(firmVO -> firmVO.getCustomerId().equals(device.getFirmId())).findFirst();
        if (first.isPresent()) {
            Optional<ParticipantVO> first1 = first.get().getParticipants().stream().findFirst();
            first1.ifPresent(participantVO -> {
                openCloseWeightVO.setParticipantId(participantVO.getParticipantId());
                if (ObjectUtils.isEmpty(openCloseWeightVO.getCostCenterId())) {
                    openCloseWeightVO.setCostCenterId(participantVO.getCostCenterId());
                    openCloseWeightVO.setCostCenterName(participantVO.getCostCenterName());
                }
            });
        }

    }
}
